package com.news.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.news.model.PageBean;
import com.news.service.PersonService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 分页展示新闻人物
 */
@WebServlet("/pageperson")
public class FindPersonPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取数据
        String rows = req.getParameter("rows");
        String currentPage = req.getParameter("currentPage");
        if (rows == null && rows.equalsIgnoreCase("")) {
            rows = "2";
        }
        if (currentPage == null && currentPage.equalsIgnoreCase("")) {
            currentPage = "1";
        }
        //2.调用Service方法
        PersonService service = new PersonService();
        PageBean pb = service.findPage(rows, currentPage);
        //3.存到request域中
        req.setAttribute("pb", pb);
        //4.转发
        req.getRequestDispatcher("/admin/IndexTemp.jsp?mainPage=personall").forward(req, resp);

        /*ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(pb);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);*/
    }
}
